import 'package:kkhousekeep/models/base_respond_entity.dart';
import 'package:kkhousekeep/models/member_list_entity.dart';
import 'package:kkhousekeep/net/base_connect.dart';

import '../tools/dialog_util.dart';
import 'api.dart';

class MeMemberConnect extends BaseConnect {
  Future<List<MemberListObjList>> getMemberList() async {
    var response = await post(Api.getMemberList, {});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value([]);
    }
    MemberListEntity entity = MemberListEntity.fromJson(response.body);
    return Future.value(entity.obj.list);
  }

  Future<BaseRespondEntity?> addMember({
    required String phone,
    required String name,
    required String iDCardType,
    required String iDCard,
    required int sex,
    required String relationship,
    required String image,
  }) async {
    var response = await post(Api.addMember, {
      'phone': phone,
      'name': name,
      'iDCardType': iDCardType,
      'iDCard': iDCard,
      'sex': sex,
      'relationship': relationship,
      'image': image,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    var entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<BaseRespondEntity?> updateMember({
    required num id,
    required String phone,
    required String name,
    required String iDCardType,
    required String iDCard,
    required int sex,
    required String relationship,
    required String image,
  }) async {
    var response = await post(Api.updateMember, {
      'id': id,
      'phone': phone,
      'name': name,
      'iDCardType': iDCardType,
      'iDCard': iDCard,
      'sex': sex,
      'relationship': relationship,
      'image': image,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    var entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }
}
